package org.example.chat.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.ibatis.cursor.Cursor;
import org.example.chat.entity.ChatGroupMemberEntity;
import org.example.chat.mapper.ChatGroupMemberMapper;
import org.example.chat.service.IChatGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.chat.entity.ChatFriendEntity;
import org.example.chat.entity.ChatUserEntity;
import org.example.chat.mapper.ChatFriendMapper;
import org.example.chat.mapper.ChatUserMapper;
import org.example.chat.service.IChatFriendService;
import org.example.chat.vo.FriendVO;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ChatFriendServiceImpl extends ServiceImpl<ChatFriendMapper, ChatFriendEntity> implements IChatFriendService {

    @Autowired
    ChatUserMapper chatUserMapper;

    @Autowired
    ChatFriendMapper chatFriendMapper;

    @Override
    public List<FriendVO> getFriendList(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("requester_id", userId);
        queryWrapper.or(" receiver_id="+userId);
        queryWrapper.eq("status", 1);
        List<ChatFriendEntity> list = this.list(queryWrapper);

        if (list.isEmpty()) {
            return List.of();
        }

        //提取出ChatFriend中的 requesterId 和 receiverId 并去重
        Set<Long> friendIds = list.stream().map(ChatFriendEntity::getRequesterId).collect(Collectors.toSet());
        friendIds.addAll(list.stream().map(ChatFriendEntity::getReceiverId).collect(Collectors.toSet()));
        //去掉自己，这样拿到的就是 好友的id 列表
        friendIds.remove(userId);

        //根据好友的ID，去查询好友的信息
        List<ChatUserEntity> chatUsers = chatUserMapper.selectListByIds(friendIds);
        //
        Map<Long, ChatUserEntity> userMap = chatUsers.stream().collect(Collectors.toMap(ChatUserEntity::getUserId, chatUser -> chatUser));
        //数据转换
        List<FriendVO> friendVOS = list.stream().map(chatFriend -> {
            FriendVO friendVO = new FriendVO();
            friendVO.setFriendId(chatFriend.getRequesterId().equals(userId) ? chatFriend.getReceiverId() : chatFriend.getRequesterId());
            friendVO.setNickname(userMap.get(friendVO.getFriendId()).getUsername());
            friendVO.setAvatar(userMap.get(friendVO.getFriendId()).getAvatar());
            friendVO.setStatus(chatFriend.getStatus().equals(1) ? "ONLINE" : "OFFLINE");
            friendVO.setRemark(chatFriend.getRemark());
            friendVO.setLastActive(userMap.get(friendVO.getFriendId()).getLastActive());
            return friendVO;
        }).collect(Collectors.toList());

        return friendVOS;
    }

    @Override
    public void sendFriendRequest(Long requesterId, Long receiverId, String remark) {

    }

    @Override
    public void agreeFriendRequest(Long friendId) {

    }

    @Override
    public void refuseFriendRequest(Long friendId) {

    }

    @Override
    public void deleteFriend(Long friendId) {

    }
}
